{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "polls = {\"sites\":[\"迈皋桥\", \"草场门\", \"浦口\", \"奥体中心\", \"仙林大学城\"], \n",
    "        \"AQI\":[55, 75, 69, 74, 58], \n",
    "        \"PM25\":[30, 54, 47,40 ,27], \n",
    "        \"SO2\":[10, 15, 13, 17, 10]}\n",
    "\n",
    "sites = {\"names\":[\"迈皋桥\", \"草场门\", \"浦口\", \"奥体中心\", \"玄武湖\"], \n",
    "        \"regions\":[\"栖霞区\", \"鼓楼区\", \"浦口区\", \"建邺区\", \"玄武区\"], \n",
    "        \"weather\":[\"晴\", \"晴\", \"多云\", \"阴\", \"晴\"]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "# 读取数据集，把字典格式转变为DataFrame格式\n",
    "df_polls = pd.DataFrame(polls)\n",
    "df_sites = pd.DataFrame(sites)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sites</th>\n",
       "      <th>AQI</th>\n",
       "      <th>PM25</th>\n",
       "      <th>SO2</th>\n",
       "      <th>names</th>\n",
       "      <th>regions</th>\n",
       "      <th>weather</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>55</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>栖霞区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>草场门</td>\n",
       "      <td>75</td>\n",
       "      <td>54</td>\n",
       "      <td>15</td>\n",
       "      <td>草场门</td>\n",
       "      <td>鼓楼区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦口</td>\n",
       "      <td>69</td>\n",
       "      <td>47</td>\n",
       "      <td>13</td>\n",
       "      <td>浦口</td>\n",
       "      <td>浦口区</td>\n",
       "      <td>多云</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>奥体中心</td>\n",
       "      <td>74</td>\n",
       "      <td>40</td>\n",
       "      <td>17</td>\n",
       "      <td>奥体中心</td>\n",
       "      <td>建邺区</td>\n",
       "      <td>阴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>仙林大学城</td>\n",
       "      <td>58</td>\n",
       "      <td>27</td>\n",
       "      <td>10</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   sites  AQI  PM25  SO2 names regions weather\n",
       "0    迈皋桥   55    30   10   迈皋桥     栖霞区       晴\n",
       "1    草场门   75    54   15   草场门     鼓楼区       晴\n",
       "2     浦口   69    47   13    浦口     浦口区      多云\n",
       "3   奥体中心   74    40   17  奥体中心     建邺区       阴\n",
       "4  仙林大学城   58    27   10   NaN     NaN     NaN"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df_polls, df_sites, left_on=\"sites\", right_on=\"names\", how=\"left\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sites</th>\n",
       "      <th>AQI</th>\n",
       "      <th>PM25</th>\n",
       "      <th>SO2</th>\n",
       "      <th>names</th>\n",
       "      <th>regions</th>\n",
       "      <th>weather</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>55.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>栖霞区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>草场门</td>\n",
       "      <td>75.0</td>\n",
       "      <td>54.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>草场门</td>\n",
       "      <td>鼓楼区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦口</td>\n",
       "      <td>69.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>浦口</td>\n",
       "      <td>浦口区</td>\n",
       "      <td>多云</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>奥体中心</td>\n",
       "      <td>74.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>奥体中心</td>\n",
       "      <td>建邺区</td>\n",
       "      <td>阴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>玄武湖</td>\n",
       "      <td>玄武区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  sites   AQI  PM25   SO2 names regions weather\n",
       "0   迈皋桥  55.0  30.0  10.0   迈皋桥     栖霞区       晴\n",
       "1   草场门  75.0  54.0  15.0   草场门     鼓楼区       晴\n",
       "2    浦口  69.0  47.0  13.0    浦口     浦口区      多云\n",
       "3  奥体中心  74.0  40.0  17.0  奥体中心     建邺区       阴\n",
       "4   NaN   NaN   NaN   NaN   玄武湖     玄武区       晴"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df_polls, df_sites, left_on=\"sites\", right_on=\"names\", how=\"right\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sites</th>\n",
       "      <th>AQI</th>\n",
       "      <th>PM25</th>\n",
       "      <th>SO2</th>\n",
       "      <th>names</th>\n",
       "      <th>regions</th>\n",
       "      <th>weather</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>55</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>栖霞区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>草场门</td>\n",
       "      <td>75</td>\n",
       "      <td>54</td>\n",
       "      <td>15</td>\n",
       "      <td>草场门</td>\n",
       "      <td>鼓楼区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦口</td>\n",
       "      <td>69</td>\n",
       "      <td>47</td>\n",
       "      <td>13</td>\n",
       "      <td>浦口</td>\n",
       "      <td>浦口区</td>\n",
       "      <td>多云</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>奥体中心</td>\n",
       "      <td>74</td>\n",
       "      <td>40</td>\n",
       "      <td>17</td>\n",
       "      <td>奥体中心</td>\n",
       "      <td>建邺区</td>\n",
       "      <td>阴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  sites  AQI  PM25  SO2 names regions weather\n",
       "0   迈皋桥   55    30   10   迈皋桥     栖霞区       晴\n",
       "1   草场门   75    54   15   草场门     鼓楼区       晴\n",
       "2    浦口   69    47   13    浦口     浦口区      多云\n",
       "3  奥体中心   74    40   17  奥体中心     建邺区       阴"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df_polls, df_sites, left_on=\"sites\", right_on=\"names\", how=\"inner\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sites</th>\n",
       "      <th>AQI</th>\n",
       "      <th>PM25</th>\n",
       "      <th>SO2</th>\n",
       "      <th>names</th>\n",
       "      <th>regions</th>\n",
       "      <th>weather</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>55.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>栖霞区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>草场门</td>\n",
       "      <td>75.0</td>\n",
       "      <td>54.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>草场门</td>\n",
       "      <td>鼓楼区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦口</td>\n",
       "      <td>69.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>浦口</td>\n",
       "      <td>浦口区</td>\n",
       "      <td>多云</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>奥体中心</td>\n",
       "      <td>74.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>奥体中心</td>\n",
       "      <td>建邺区</td>\n",
       "      <td>阴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>仙林大学城</td>\n",
       "      <td>58.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>玄武湖</td>\n",
       "      <td>玄武区</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   sites   AQI  PM25   SO2 names regions weather\n",
       "0    迈皋桥  55.0  30.0  10.0   迈皋桥     栖霞区       晴\n",
       "1    草场门  75.0  54.0  15.0   草场门     鼓楼区       晴\n",
       "2     浦口  69.0  47.0  13.0    浦口     浦口区      多云\n",
       "3   奥体中心  74.0  40.0  17.0  奥体中心     建邺区       阴\n",
       "4  仙林大学城  58.0  27.0  10.0   NaN     NaN     NaN\n",
       "5    NaN   NaN   NaN   NaN   玄武湖     玄武区       晴"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df_polls, df_sites, left_on=\"sites\", right_on=\"names\", how=\"outer\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义左侧数据集\n",
    "df_left = pd.DataFrame({\"sites\":[\"迈皋桥\", \"草场门\", \"浦口\", \"迈皋桥\", \"草场门\", \"浦口\"], \n",
    "                       \"date\":[\"2019-12-08\", \"2019-12-08\", \"2019-12-08\", \"2019-12-07\", \"2019-12-07\", \"2019-12-07\"],  \n",
    "                       \"AQI\":[55, 75, 69, 45, 35, 53]})\n",
    "# 定义右侧数据集\n",
    "df_right = pd.DataFrame({\"sites\":[\"迈皋桥\", \"草场门\", \"迈皋桥\", \"草场门\"], \n",
    "                        \"date\":[\"2019-12-08\", \"2019-12-08\", \"2019-12-09\", \"2019-12-09\"], \n",
    "                        \"weather\":[\"晴\",  \"晴\", \"多云\", \"阴\"]})\n",
    "\n",
    "sites_with_index = pd.DataFrame({\"regions\":[\"栖霞区\", \"鼓楼区\", \"浦口区\", \"建邺区\", \"玄武区\"]},\n",
    "                                index=[\"迈皋桥\", \"草场门\", \"浦口\", \"奥体中心\", \"玄武湖\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sites</th>\n",
       "      <th>date</th>\n",
       "      <th>AQI</th>\n",
       "      <th>weather</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>2019-12-08</td>\n",
       "      <td>55</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>草场门</td>\n",
       "      <td>2019-12-08</td>\n",
       "      <td>75</td>\n",
       "      <td>晴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  sites        date  AQI weather\n",
       "0   迈皋桥  2019-12-08   55       晴\n",
       "1   草场门  2019-12-08   75       晴"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df_left, df_right, on=[\"sites\", \"date\"], how=\"inner\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sites</th>\n",
       "      <th>AQI</th>\n",
       "      <th>PM25</th>\n",
       "      <th>SO2</th>\n",
       "      <th>regions</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>55</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "      <td>栖霞区</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>草场门</td>\n",
       "      <td>75</td>\n",
       "      <td>54</td>\n",
       "      <td>15</td>\n",
       "      <td>鼓楼区</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦口</td>\n",
       "      <td>69</td>\n",
       "      <td>47</td>\n",
       "      <td>13</td>\n",
       "      <td>浦口区</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>奥体中心</td>\n",
       "      <td>74</td>\n",
       "      <td>40</td>\n",
       "      <td>17</td>\n",
       "      <td>建邺区</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  sites  AQI  PM25  SO2 regions\n",
       "0   迈皋桥   55    30   10     栖霞区\n",
       "1   草场门   75    54   15     鼓楼区\n",
       "2    浦口   69    47   13     浦口区\n",
       "3  奥体中心   74    40   17     建邺区"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(df_polls, sites_with_index, left_on='sites', right_index=True, how=\"inner\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_polls = {\"sites\":[\"迈皋桥\", \"草场门\", \"浦口\", \"奥体中心\", \"仙林大学城\"], \n",
    "        \"AQI\":[65, 85, 79, 78, 78], \n",
    "        \"PM25\":[50, 74, 67,60 ,47], \n",
    "        \"O2\":[20, 35, 23, 37, 15]}\n",
    "        \n",
    "polls = {\"sites\":[\"迈皋桥\", \"草场门\", \"浦口\", \"奥体中心\", \"仙林大学城\"], \n",
    "        \"AQI\":[55, 75, 69, 74, 58], \n",
    "        \"PM25\":[30, 54, 47,40 ,27], \n",
    "        \"SO2\":[10, 15, 13, 17, 10]}\n",
    "\n",
    "\n",
    "df_polls_new = pd.DataFrame(new_polls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>sites</th>\n",
       "      <th>AQI</th>\n",
       "      <th>PM25</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2019-12-08</th>\n",
       "      <th>0</th>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>55</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>草场门</td>\n",
       "      <td>75</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦口</td>\n",
       "      <td>69</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>奥体中心</td>\n",
       "      <td>74</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>仙林大学城</td>\n",
       "      <td>58</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2019-12-07</th>\n",
       "      <th>0</th>\n",
       "      <td>迈皋桥</td>\n",
       "      <td>65</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>草场门</td>\n",
       "      <td>85</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>浦口</td>\n",
       "      <td>79</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>奥体中心</td>\n",
       "      <td>78</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>仙林大学城</td>\n",
       "      <td>78</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              sites  AQI  PM25\n",
       "2019-12-08 0    迈皋桥   55    30\n",
       "           1    草场门   75    54\n",
       "           2     浦口   69    47\n",
       "           3   奥体中心   74    40\n",
       "           4  仙林大学城   58    27\n",
       "2019-12-07 0    迈皋桥   65    50\n",
       "           1    草场门   85    74\n",
       "           2     浦口   79    67\n",
       "           3   奥体中心   78    60\n",
       "           4  仙林大学城   78    47"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([df_polls, df_polls_new], join=\"inner\", keys=[\"2019-12-08\", \"2019-12-07\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name\n",
       "A     4\n",
       "B     5\n",
       "C    12\n",
       "Name: value, dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 定义数据集\n",
    "df = pd.DataFrame({\"name\":[\"A\", \"B\", \"C\", \"B\", \"C\", \"A\", \"C\"], \"value\":[1, 3, 7, 2, 4, 3, 1]})\n",
    "df.groupby(\"name\")[\"value\"].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "  name  value\n",
      "0    A      1\n",
      "5    A      3\n",
      "#################\n",
      "B\n",
      "  name  value\n",
      "1    B      3\n",
      "3    B      2\n",
      "#################\n",
      "C\n",
      "  name  value\n",
      "2    C      7\n",
      "4    C      4\n",
      "6    C      1\n",
      "#################\n"
     ]
    }
   ],
   "source": [
    "grouped = df.groupby(\"name\")\n",
    "for name, group in grouped:\n",
    "    print(name)\n",
    "    print(group)\n",
    "    print(\"#################\")"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
  },
  "kernelspec": {
   "display_name": "Python 3.9.1 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
